package com.hg.library.ui.gallery;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;

/**
 * Created by ASUS-PC on 2016/4/14.
 */
public class GalleryManager {

    private Camera mCamera = new Camera();

    private int mRotationAngle = 40;

    private int mMaxZoom = 0;

    private int mCoveflowCenter = 0;

    private int centerOffset = 0;

    private Gallery gallery;

    public GalleryManager(Gallery gallery) {
        this.gallery = gallery;
    }

    public int ChildDrawingOrder(int childCount, int i) {
        int selectedIndex = gallery.getSelectedItemPosition() - gallery.getFirstVisiblePosition();
        if (i < selectedIndex) return i;
        else return childCount - 1 - i + selectedIndex;
    }

    public boolean ChildStaticTransformation(View child, Transformation t) {
        final int childCenter = getCenterOfView(child);
        final int childWidth = child.getWidth();

        int rotationAngle = 0;
        t.clear();
        t.setTransformationType(Transformation.TYPE_MATRIX);

        if (child.isSelected()) {
            transformImageBitmap(child, t, centerOffset);
        } else {
            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mRotationAngle);

            rotationAngle = rotationAngle > 0 ? getRotationAngle():-getRotationAngle();

//            if (Math.abs(rotationAngle) > mRotationAngle) {
//                rotationAngle = (rotationAngle < 0) ? -mRotationAngle : mRotationAngle;
//            }

            transformImageBitmap(child, t, rotationAngle);
        }
        return true;
    }

    public void setCenterOffset(int centerOffset) {
        this.centerOffset = centerOffset;
    }

    public void onSizeChanged() {
        mCoveflowCenter = getCenterOfCoverflow();
    }

    public int getCenterOfCoverflow() {
        return (gallery.getWidth() - gallery.getPaddingLeft() - gallery.getPaddingRight()) / 2 + gallery.getPaddingLeft();
    }

    /*
    获取到的坐标不对，以后看看是什么原因
     */
    private int getCenterOfView(View view) {
        int[] local = new int[2];
        view.getLocationOnScreen(local);
        return local[0] + view.getWidth() / 2;
    }

    private void transformImageBitmap(View child, Transformation t, int rotationAngle) {
        mCamera.save();

        final Matrix imageMatrix = t.getMatrix();
        final int imageHeight = child.getHeight();
        final int imageWidth = child.getWidth();
        final int rotation = Math.abs(rotationAngle);

        mCamera.translate(0, 0, mMaxZoom);

        if (rotation < mRotationAngle) {
            float zoomAmount = rotation * 1.2f;
            mCamera.translate(0, 0, zoomAmount);
        }

        mCamera.rotateY(rotationAngle);
        mCamera.getMatrix(imageMatrix);

        imageMatrix.postScale(0.6f,0.8f);
        imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
        imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));

        mCamera.restore();
    }

    public int getRotationAngle() {
        return mRotationAngle;
    }

    public void setRotationAngle(int maxRotationAngle) {
        mRotationAngle = maxRotationAngle;
    }

    public int getMaxZoom() {
        return mMaxZoom;
    }

    public void setMaxZoom(int maxZoom) {
        mMaxZoom = maxZoom;
    }

}
